var API = require('API').vehicle_model;
var select_brand = '<select ng-show="level >= 0" ng-disabled="disabled || lock_brand" ng-model="vehicleModel.brand" ng-change="brand_chg()" ng-options="brand for brand in brands"></select>';
var select_model = '<select ng-show="level >= 1" ng-disabled="disabled || !show_model" ng-model="vehicleModel.model" ng-change="model_chg()" ng-options="model for model in models"></select>';
var select_version = '<select ng-show="level >= 2 " ng-disabled="disabled || !show_version" ng-model="vehicleModel.version" ng-change="version_chg()" ng-options="version for version in versions"></select>';
module.exports = function () {
    return {
        restrict  : 'E',
        scope     : {
            vehicleModel: "=",
            disabled: "=?ngDisabled"
        },
        template  : '<div style="cursor: default">' + select_brand + select_model + select_version + '</div>',
        replace   : true,
        controller: ['$scope', '$element', function ($scope, $element) {

            $scope.level = Number($element.attr('level')) || 2;

            (function reset() {
                $scope.show_input = true;
                $scope.show_vehicle = false;
                $scope.brands = [];
                $scope.show_brands = false;

                $scope.models = [];
                $scope.show_model = false;
                $scope.show_sn = false;
                $scope.selected_version = {};
                API.brands(function (brands) {
                    $scope.$apply(function () {
                        $scope.brands = brands.map(function (x) {
                            return x.brand;
                        });
                        bind();
                    })
                });

            })();

            $scope.$watch('vehicleModel', function (val, old) {
                if (val && val !== old) {
                    bind();
                }
            });

            function bind() {
                if(!$scope.vehicleModel)return;
                if ($scope.vehicleModel.brand && $scope.vehicleModel.model && $scope.vehicleModel.version) {
                    var model = $scope.vehicleModel.model;
                    var version = $scope.vehicleModel.version;
                    $scope.brand_chg($scope.vehicleModel.brand, function () {
                        $scope.model_chg(model, function () {
                            $scope.vehicleModel.version = version;
                            $scope.version_chg();
                        });
                    });
                } else if ($scope.vehicleModel.brand && $scope.vehicleModel.model) {
                    var mem = $scope.vehicleModel.model;
                    $scope.brand_chg($scope.vehicleModel.brand, function () {
                        $scope.model_chg(mem);
                    })
                } else if ($scope.vehicleModel.brand) {
                    $scope.brand_chg();
                }
            }

            $scope.brand_chg = function (brand, then) {

                var b = $scope.vehicleModel.brand || ($scope.vehicleModel.brand = brand);

                $scope.show_model = false;
                $scope.show_version = false;

                API.models_for_brand(b, function (models) {
                    $scope.$apply(function () {
                        $scope.models = models.map(function (x) {
                            return x.model
                        });
                        $scope.show_model = true;
                        $scope.vehicleModel.model = void 0;
                        $scope.vehicleModel.version = void 0;
                        if (then)then();
                    });
                });

            };

            $scope.model_chg = function (model, then) {

                var b = $scope.vehicleModel.brand;
                var m = $scope.vehicleModel.model || ($scope.vehicleModel.model = model);
                //$scope.show_version = false;

                API.versions_for_brand_and_model(b, m, function (versions) {
                    $scope.$apply(function () {
                        $scope.versions = versions.map(function (x) {
                            return x.version
                        });
                        $scope.show_version = true;

                        $scope.vehicleModel.version = void 0;
                        if (then)then();
                    });
                });
            };

            $scope.version_chg = function (model) {
                API.search_by_brand_model_version(model || $scope.vehicleModel,
                    function (vms) {
                        if (vms.length > 0) {
                            var complete_vm = vms[0];
                            $scope.$apply(function () {
                                $.extend($scope.vehicleModel, complete_vm);
                                $scope.show_version = true;
                                $scope.show_model = true;
                                //console.log(complete_vm);
                            });
                        }
                    })
            };

        }]
    }
};
